<%@ Page Language="C#" MasterPageFile="~/FiledbookMaster.Master" AutoEventWireup="true" CodeBehind="FieldSpecies.aspx.cs" Inherits="Fieldbook.Testwebsite.User.FieldSpecies" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <script type="text/javascript">
        function ShowHideDiv(on, elemID)
        {
            var divElem = document.getElementById(elemID);
            if (on)
            {
                divElem.style.display = "block";
            }
            else 
            {
                divElem.style.display = "none";
            }
            return false;
        }
        
        var allPlantSpecies = new Array(
           <%= Fieldbook.Business.JSONSerializator<Fieldbook.Entities.PlantSpecie>.ListToJson(this.allSpecies, "PlantSpecieID", "CategoryID", "Description") %>
        ) 
                
        function populateFieldSpecies()
        {
            var selObjCategoryID = document.getElementById('<%= this.selectPlantSpecieCategory.ClientID %>');
            var selectedCategory = selObjCategoryID.options[selObjCategoryID.selectedIndex].value;
            var selObjSpecie = document.getElementById('selectSpecieID');
            selObjSpecie.options.length=0;
            for(var i=0; i<allPlantSpecies.length; i++)
            {
                if (selectedCategory == allPlantSpecies[i].CategoryID)
                {
                    var option = document.createElement("OPTION");
                    option.text = allPlantSpecies[i].Description;
                    option.value = allPlantSpecies[i].PlantSpecieID;
                    selObjSpecie.options.add(option);
                }
            }
        }
        
    </script>
    <a href="#" onclick="return ShowHideDiv(true, 'divAddFieldPlantSpecie')">Add plant specie</a>
    <br />
    <br />
    <asp:Label ID="lblOperationStatus" runat="server" EnableViewState="false"/>
    <div id="divAddFieldPlantSpecie" style="display:none;">
        <table>
            <tr>
                <td>Category: </td>
                <td>
                   <select id="selectPlantSpecieCategory" runat="server"
                        onchange="populateFieldSpecies();"/>
                </td>
            </tr>
            <tr>
                <td>
                    Specie
                </td>
                <td>
                    <select id="selectSpecieID" name="selectSpecieID" ></select>
                </td>
            </tr>
            <tr>
                <td>
                    Sort
                </td>
                <td>
                    <asp:TextBox ID="txtSort" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                    Specie start date
                </td>
                <td>
                    <asp:TextBox ID="txtSpecieStartDate" runat="server" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:LinkButton id="linkAddFieldSpecie" runat="Server" OnCommand="linkAddFieldSpecie_Command" Text="Add"/>
                    <a href="#" onclick="return ShowHideDiv(false, 'divAddFieldPlantSpecie')">Cancel</a>                        
                </td>
            </tr>               
        </table>
    </div>
    
    <asp:GridView ID="dgvFieldWorks" runat="server" AutoGenerateColumns="false" 
        AllowPaging="True"
        DataSourceID="FieldSpeciesODS">
        <Columns>
            <asp:BoundField DataField="Sort" HeaderText="Sort" SortExpression="Sort" />
            <asp:BoundField DataField="SpecieStartDate" HeaderText="SpecieStartDate" SortExpression="SpecieStartDate"  DataFormatString="{0: dd/MM/yyyy}"/>
        </Columns>
    </asp:GridView>
    <asp:ObjectDataSource ID="FieldSpeciesODS" runat="server" TypeName="Fieldbook.Business.PlantSpecieController"
        SelectMethod="LoadFieldSpecies">
        <SelectParameters>
            <asp:SessionParameter Name="userID" SessionField="userID" Type="int64" />
            <asp:QueryStringParameter Name="fieldID" QueryStringField="fieldID" Type="int64" />
        </SelectParameters>
    </asp:ObjectDataSource>
        
    <script type="text/javascript">
        populateFieldSpecies();
    </script>
    
</asp:Content>
